﻿using Messhall.Framework.Domain.DomainObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Messhall.Domain.Permission.IRepositorys;
using Messhall.Framework.Autofac;

namespace Messhall.Domain.Permission.BusinessObjects
{
    public class PermissionEntity : EntityBoBase
    {
        #region 成员变量

        /// <summary>
        /// 父级权限
        /// </summary>
        public Guid ParentId { get; set; }

        /// <summary>
        /// 权限唯一CODE代码
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 权限名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// URL规则
        /// </summary>
        public string Uri { get; set; }

        /// <summary>
        /// 图标
        /// </summary>
        public string Icon { get; set; }

        /// <summary>
        /// 备注信息
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// 权限code码
        /// </summary>
        public List<PermissionCodeEntity> PermissionCodes { get; set; }

        #endregion
    }

    public class Permission : BoBase, IAggregateRoot
    {
        private PermissionEntity SelfEntity
        {
            get { return Entity as PermissionEntity; }
            set { this.Entity = value; }
        }

        public Permission(Guid parentId, string icon, string name, string code, string uri, string remark)
        {
            this.SelfEntity = new PermissionEntity()
            {
                ParentId = parentId,
                Code = code,
                Name = name,
                Uri = uri,
                Remark = remark,
                Icon = icon
            };
        }

        public Permission(PermissionEntity entity)
        {
            this.SelfEntity = entity;
        }

        private List<PermissionCode> _codes;

        public async Task<List<PermissionCode>> GetAuthoritys()
        {
            var que = this.SelfEntity.PermissionCodes.AsQueryable();
            if (_codes == null)
            {
                //var permissionCode = await AutofacBuilder.Resolve<IPermissionCodeRepository>()
                //    .FindAsync(code => code.PermissionKeyId == this.SelfEntity.KeyId);

                _codes = new List<PermissionCode>();

            }

            foreach (var item in this.SelfEntity.PermissionCodes)
            {
                _codes.Add(new(item));
            }

            return _codes;
        }

        public void SetAuthority(string name, string code)
        {
            if (this.SelfEntity.PermissionCodes == null)
            {
                this.SelfEntity.PermissionCodes = new List<PermissionCodeEntity>();
            }

            var permissionCode = new PermissionCode(this.GetKeyId(), name, code);

            this.SelfEntity.PermissionCodes.Add((PermissionCodeEntity)permissionCode.GetEntity());

            //var permissionCodeRepository =  AutofacBuilder.Resolve<IPermissionCodeRepository>();

            //permissionCodeRepository.AddAsync(permissionCode);
            //_codes.Add(permissionCode);
        }

        public Guid GetParentId()
        {
            return this.SelfEntity.ParentId;
        }

        public void SetParentId(Guid parentId)
        {
            this.SelfEntity.ParentId = parentId;
        }

        public string GetName()
        {
            return this.SelfEntity.Name;
        }

        public void SetName(string name)
        {
            this.SelfEntity.Name = name;
        }

        public string GetCode()
        {
            return this.SelfEntity.Code;
        }

        public void SetCode(string code)
        {
            this.SelfEntity.Code = code;
        }

        public string GetUri()
        {
            return this.SelfEntity.Uri;
        }

        public void SetUri(string uri)
        {
            this.SelfEntity.Uri = uri;
        }

        public string GetRemark()
        {
            return this.SelfEntity.Remark;
        }

        public void SetRemark(string remark)
        {
            this.SelfEntity.Remark = remark;
        }

        public string GetIcon()
        {
            return this.SelfEntity.Icon;
        }

        public void SetIcon(string icon)
        {
            this.SelfEntity.Icon = icon;
        }
    }
}